package com.easy.easylibdemo.ui.listedit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.easy.easylibdemo.R;
import com.easylib.utils.KeyBoardUtils;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ListEdit2Activity extends AppCompatActivity {
    @Bind(R.id.list)
    RecyclerView recyclerView;
    int pos=-1;
    int cbPos=-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_edit);
        ButterKnife.bind(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(ListEdit2Activity.this,LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(new RecyclerView.Adapter() {

            SparseArray<Boolean> watchNeedCreateAry=new SparseArray();
            SparseArray<String> etTextAry=new SparseArray();
            SparseArray<TextWatcher> textWatcherAry=new SparseArray();
            HashMap<Integer,RadioButton> cbAry=new HashMap();
            TextWatcher textWatcher;

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View itemLayout = LayoutInflater.from(ListEdit2Activity.this).inflate(
                  R.layout.item_edit, parent,false);
                return new Mholder(itemLayout);
            }



            @Override
            public void onViewRecycled(RecyclerView.ViewHolder holder) {
                super.onViewRecycled(holder);
                Log.e("tag", "onViewRecycled==");
                Mholder viewHolder=(Mholder)holder;
                viewHolder.et.removeTextChangedListener(textWatcherAry.get(holder.getLayoutPosition()));
                textWatcherAry.remove(holder.getLayoutPosition());
//                cbAry.remove(holder.getLayoutPosition());
            }




            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int ii) {
                final int i=ii;
                Log.e("tag", i + "convert==");
                final Mholder viewHolder=(Mholder)holder;
                viewHolder.tv.setText(i+"");
                viewHolder.et.setText(etTextAry.get(i));
                if (pos == i) {
                    viewHolder.et.requestFocus();
                    viewHolder.et.setSelection(viewHolder.et.getText().length());
                }
//                if (watchNeedCreateAry.get(i,true)){
//                Log.e("tag", i + "new TextWatcher==");
                textWatcher = new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                            Log.e("tag", i + "beforeTextChanged==" + s);
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
//                            Log.e("tag",i+ "onTextChanged==" + s);
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
//                        Log.e("tag",i+ "afterTextChanged==" + s );
                        etTextAry.put(i,s.toString());
                    }
                };
                viewHolder.et.addTextChangedListener(textWatcher);
                textWatcherAry.put(i,textWatcher);
//                    watchNeedCreateAry.put(i,false);
//                }

                viewHolder.et.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
//                            etPos = position;
                            pos = i;
                            Log.e("tag","pos=="+pos);
                        }
                        return false;
                    }
                });


                cbAry.put(i,viewHolder.cb);
                if (cbPos==i){
                    Log.e("tag",i+"==i==cbPos"+cbPos);
                    viewHolder.cb.setChecked(true);
                }else{
                    viewHolder.cb.setChecked(false);
                }
                viewHolder.root.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cbPos=i;
                        for (int j = 0; j < cbAry.size(); j++) {
                            if (j!=cbPos)
                                cbAry.get(j).setChecked(false);
                        }
                        cbAry.get(i).setChecked(true);
                    }
                });

            }

            @Override
            public int getItemCount() {
                return 40;
            }


            @Override
            public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
                super.onViewDetachedFromWindow(holder);
                Log.e("tag",pos+"==onViewDetachedFromWindow=getLayoutPosition=" + holder.getLayoutPosition() );
                if (pos==holder.getLayoutPosition()){
//                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        KeyBoardUtils.closeKeybord(((Mholder)holder).et);
//                    inputMethodManager.hideSoftInputFromWindow(((Mholder)holder).et.getWindowToken(), 0);
                }
            }
//            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            @Override
            public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
                super.onViewAttachedToWindow(holder);
                Log.e("tag",pos+"==onViewAttachedToWindow=getLayoutPosition=" + holder.getLayoutPosition() );
                if (pos==holder.getLayoutPosition()){

                        KeyBoardUtils.openKeybord(((Mholder)holder).et);
//                    inputMethodManager.showSoftInput(((Mholder)holder).et, 0);
                }
            }

            class Mholder extends RecyclerView.ViewHolder{

                private TextView tv;
                private EditText et;
                private RadioButton cb;
                private LinearLayout root;

                public Mholder(View itemView) {
                    super(itemView);
                    tv=(TextView)itemView.findViewById(R.id.tv_name);
                    et=(EditText)itemView.findViewById(R.id.et_text);
                    cb=(RadioButton)itemView.findViewById(R.id.cb_text);
                    root=(LinearLayout)itemView.findViewById(R.id.ll_root);
                }


            }
        });
    }
}
